<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：团队等级控制器。
 *
 **/
namespace Admin\Controller;

class TeamController extends ComController
{
    /**
     * 列表
     */
    public function index()
    {
        $list = M('team_level')->order('o asc')->select();
        foreach ($list as $k=>&$v){
            $user_level = M('user_level')->where('id='.$v['user_level_id'])->find();
            $list[$k]['user_level_name'] = $user_level['title'];
        }
        $this->assign('list',$list);
        $this->display();
    }
    
    /**
     * 新增
     */
    public function add()
    {
        if(!IS_POST){
            $user_level_list = M('user_level')->order('o asc')->where(array('is_r'=>1,'price'=>array('gt',0)))->select();
            $this->assign('user_level_list',$user_level_list);
            $this->assign('method',U('Team/add'));
            $this->display('form');
        }else{
            $data['name'] = I('post.name','','htmlspecialchars');
            $data['min_num'] = I('post.min_num',1);
            $data['user_level_id'] = I('post.user_level_id','');
            $data['o'] = I('post.o',1);
            $data['t'] = time();
            if(!$data['name'] or !$data['min_num'] or !$data['user_level_id'] or !$data['o']){
                $this->error('表单项存在空项，请检查后重新提交！');
            }
            
            $res = M('team_level')->data($data)->add();
            if($res){
                addlog('新增团队等级，ID：'.$res);
                $this->success('添加成功！',U('index'));
            }else{
                $this->error('操作失败，请稍后再试');
            }
        }
    }
    
    /**
     * 修改
     */
    public function update()
    {
        $id = I('id');
        if(!$id){
            $this->error('传参为空');
        }
        if(!IS_POST){
            $user_level_list = M('user_level')->order('o asc')->where(array('is_r'=>1,'price'=>array('gt',0)))->select();
            $vo = M('team_level')->where('id='.$id)->find();
            $this->assign('vo',$vo);
            $this->assign('user_level_list',$user_level_list);
            $this->assign('method',U('Team/update'));
            $this->display('form');
        }else{
            $data['name'] = I('post.name','','htmlspecialchars');
            $data['min_num'] = I('post.min_num',1);
            $data['user_level_id'] = I('post.user_level_id','');
            $data['o'] = I('post.o',1);
            $data['t'] = time();
            if(!$data['name'] or !$data['min_num'] or !$data['user_level_id'] or !$data['o']){
                $this->error('表单项存在空项，请检查后重新提交！');
            }
            
            $res = M('team_level')->data($data)->where('id='.$id)->save();
            if($res){
                addlog('修改团队等级，ID：'.$id);
                $this->success('修改成功！',U('index'));
            }else{
                $this->error('操作失败，请稍后再试');
            }
        }
    }
    
    /**
     * 删除
     */
    public function delete()
    {
        $id = I('get.id');
        if(!$id){
            $this->error('传参为空');
        }
        $res = M('team_level')->where('id='.$id)->delete();
        if($res){
            addlog('删除团队等级，ID：'.$id);
            $this->success('删除团队等级成功',U('index'));
        }else{
            $this->error('操作失败，请稍后再试');
        }
    }
}